package cn.interestingshop.utils;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * @ClassName : JedisPoolUtils
 * @Description :
 * @Author : Lzz
 * @Date: 2023/3/3 15:59:12
 */
public class JedisPoolUtils {

    private static JedisPool pool;

    static{
        //1、加载配置文件
        Properties properties = new Properties();
        try {
            //2、加载配置文件
            InputStream is = JedisPoolUtils.class.getClassLoader().getResourceAsStream("jedis.properties");
            properties.load(is);
            //3、初始化连接池对象
            String host = properties.getProperty("host");
            String port = properties.getProperty("port");
            String maxTotal = properties.getProperty("maxTotal");
            String maxIdel = properties.getProperty("maxIdle");
            String maxWaitMillis = properties.getProperty("maxWaitMillis");
            //4、创建Jedis连接池的配置类对象
            JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
            jedisPoolConfig.setMaxActive(Integer.parseInt(maxTotal));
            jedisPoolConfig.setMaxIdle(Integer.parseInt(maxIdel));
            jedisPoolConfig.setMaxWait(Integer.parseInt(maxWaitMillis));
            //5、创建Jedis连接对象
            pool = new JedisPool(jedisPoolConfig, host,Integer.parseInt(port));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取Jedis对象
     * @return
     */
    public static Jedis getJedis(){
        Jedis jedis = null;
        if(pool != null){
            jedis = pool.getResource();
        }
        return jedis;
    }





}
